import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Transfer extends JPanel implements ActionListener {//转账
    private JButton backBtn;
    private JButton backBtn1;
    private Start frame;
    private Icon anj = Util.getIcon("/images/buttonOff.jpg");
    private Icon anj1 = Util.getIcon("/images/buttonSure.jpg");
    private JTextField zhaM = new JTextField();
    private JTextField zhaName = new JTextField();

    public JTextField getZhaName() {
        return zhaName;
    }

    public void setZhaName(JTextField zhaName) {
        this.zhaName = zhaName;
    }

    public JTextField getZhaM() {
        return zhaM;
    }

    public void setZhaM(JTextField zhaM) {
        this.zhaM = zhaM;
    }

    public Transfer(Start frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        JLabel titleLabel = new JLabel("转账");
        titleLabel.setBounds(380, 10, 100, 50);
        this.add(titleLabel);

        JLabel zhn = new JLabel("转入账户：");
        zhn.setBounds(250, 200, 150, 20);
        zhn.setForeground(Color.WHITE);
        zhn.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(zhn);

        zhaName.setBounds(350, 200, 100, 20);
        this.add(zhaName);

        JLabel zhm = new JLabel("转入金额：");
        zhm.setBounds(250, 250, 150, 20);
        zhm.setForeground(Color.WHITE);
        zhm.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(zhm);

        zhaM.setBounds(350, 250, 100, 20);
        this.add(zhaM);

        backBtn = new JButton(anj);
        backBtn.setBounds(450, 350, 100, 50);
        this.add(backBtn);
        backBtn.addActionListener(this);

        backBtn1 = new JButton(anj1);
        backBtn1.setBounds(250, 350, 100, 50);
        this.add(backBtn1);
        backBtn1.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (backBtn == e.getSource()) {
            frame.switchPanel("menu");
        } else if (backBtn1 == e.getSource()) {
            for (int i = 0; i < frame.getUser().getInformation().size(); i++) {
                if (frame.getZha().getZhaName().getText().equals(frame.getUser().getInformation().get(i).getAccount())) {
                    if (frame.getUser().getInformation().get(frame.getLoginPanel().getInd()).getAccount().equals(frame.getZha().getZhaName().getText())){
                        JOptionPane.showMessageDialog(this,"不能给自己转账");
                    }else {
                        double zmoNey=Double.valueOf(frame.getZha().getZhaM().getText())+frame.getUser().getInformation().get(i).getBalance();
                        frame.getUser().getInformation().get(i).setBalance(zmoNey);
                        JOptionPane.showMessageDialog(this,"转账成功");
                        frame.switchPanel("menu");
                    }
                }else {
                    JOptionPane.showMessageDialog(this,"查无此人");
                    break;
                }
            }
        }
    }
}
